'use strict';
const Controller = require('egg').Controller;

class GoodsController extends Controller {
  async newest() {
    let { ctx, service } = this;
    let { limit } = ctx.query;
    let end = await service.goods.newest(limit);
    ctx.body = end;
  }
  async hotest() {
    let { ctx, service } = this;
    let { limit } = ctx.query;
    let end = await service.goods.hotest(limit);
    ctx.body = end;
  }
  async byBrandSeries() {
    let { ctx, service } = this;
    let { brandId, seriesId, currPage, pageSize, orderBy, orderType } = ctx.query;
    let end = await service.goods.byBrandSeries(brandId, seriesId, currPage, pageSize, orderBy, orderType);
    ctx.body = end;
  }
  async detail() {
    let { ctx, service } = this;
    let { goodsId } = ctx.query;
    let end = await service.goods.detail(goodsId);
    ctx.body = end;
  }
  async params() {
    let { ctx, service } = this;
    let { goodsId } = ctx.query;
    let end = await service.goods.params(goodsId);
    ctx.body = end;
  }
  async comment() {
    let { ctx, service } = this;
    let { goodsId, currPage, pageSize, stars, hasCont, hasImg } = ctx.query;
    let end = await service.goods.comment(goodsId, currPage, pageSize, stars, hasCont, hasImg);
    ctx.body = end;
  }
  async searchByWords() {
    let { ctx, service } = this;
    let { content, currPage, pageSize, orderBy, orderType } = ctx.query;
    let end = await service.goods.searchByWords(content, currPage, pageSize, orderBy, orderType);
    ctx.body = end;
  }
  async searchByOpts() {
    let { ctx, service } = this;
    let { brandId, price, screen, ram, rom, currPage, pageSize, orderBy, orderType } = ctx.query;
    let end = await service.goods.searchByOpts(brandId, price, screen, ram, rom, currPage, pageSize, orderBy, orderType);
    ctx.body = end;
  }
  async shopCart() {
    let { ctx, service } = this;
    let { userId, currPage, pageSize } = ctx.query;
    let end = await service.goods.shopCart(userId, currPage, pageSize);
    ctx.body = end;
  }
  async shopCartAdd() {
    let { ctx, service } = this;
    let code = 1, msg = `成功`;
    let { userId, goodsStyleId, setMeal, amount } = ctx.query;
    let end = await service.goods.shopCartAdd(userId, goodsStyleId, setMeal, amount);
    if (end.affectedRows == 0) {
      code = 0;
      msg = "失败"
    }
    ctx.body = service.utility.responseBody(code, msg);
  }
  async shopCartOut() {
    let { ctx, service } = this;
    let code = 1, msg = `成功`;
    let { type, id } = ctx.query;
    let end = await service.goods.shopCartOut(type, id);
    if (end.affectedRows == 0) {
      code = 0;
      msg = "失败"
    }
    ctx.body = service.utility.responseBody(code, msg);
  }
  async deal() {
    let { ctx, service } = this;
    let { userId, status, currPage, pageSize } = ctx.query;
    let end = await service.goods.deal(userId, status, currPage, pageSize);
    ctx.body = end;
  }
}

module.exports = GoodsController;
